## Authors: Alexander Herzog and Kenneth Benoit
## Date: May 30, 2015
## Replication file for JOP article "The Most Unkindest Cuts: Speaker Selection and Expressed Government Dissent During Economic Crisis"

rm(list = ls(all = TRUE))

library(xtable)
library(ggplot2)
library(reshape2)

### PATHS ########################################
dataFile <- "./generated_data/working_data_all.RData"
##################################################

load(dataFile)
data <- dataAll

data$opposition <- 0
data$opposition[data$position=="Opposition"] <- 1


# election dats
election.dates <- c("1987-02-17", "1989-06-15", "1992-11-25", "1997-06-06", "2002-05-17", "2007-05-24", "2011-02-25")


# ==========================================
# = Plot for speaker composition over time =
# ==========================================

# table frame
t <- data.frame(budget.year=names(table(data$budget_year)), N=166)

# number of speakers
t <- data.frame(t, NS=with(data, aggregate(spoke, list(budget_year), sum))[,2])

# number of cabinet members
t <- data.frame(t, C=with(data, aggregate(cabMember, list(budget_year), sum))[,2])

# proportion of cabinet members
t$CP <- t$C/t$N

# number of cabinet members who spoke
t <- data.frame(t, CS=with(data[data$spoke==1, ], aggregate(cabMember, list(budget_year), sum))[,2])

# proportion of cabinet members who spoke
t$CSP <- t$CS/t$NS

# number of backbenchers
t <- data.frame(t, B=with(data, aggregate(backbench, list(budget_year), sum))[,2])

# proportion of backbenchers
t$BP <- t$B/t$N

# number of backbenchers who spoke
t <- data.frame(t, BS=with(data[data$spoke==1, ], aggregate(backbench, list(budget_year), sum))[,2])

# proportion of backbenchers who spoke
t$BSP <- t$BS/t$NS

# number of opposition members
t <- data.frame(t, O=with(data, aggregate(opposition, list(budget_year), sum))[,2])

# proportion of opposition members
t$OP <- t$O/t$N

# number of opposition members who spoke
t <- data.frame(t, OS=with(data[data$spoke==1, ], aggregate(opposition, list(budget_year), sum))[,2])

# proportion of opposition members who spoke
t$OSP <- t$OS/t$NS


# Plot log odds ratios over time
# ------------------------------
# calculate ratio of speakers in each group
# Note: A value >1 means that a groups' proportion of speakers is larger than the proportion of that group among all TDs
t$CR <- t$CSP/t$CP
t$BR <- t$BSP/t$BP
t$OR <- t$OSP/t$OP

# convert ratios from wide to long
ratios <- melt(t[, c("budget.year", "CR", "BR", "OR")])
names(ratios) <- c("budget.year","group","odds.ratio")

levels(ratios$group) <- c("Cabinet", "Backbench", "Opposition")
ratios$group <- factor(ratios$group, levels=c("Opposition", "Backbench", "Cabinet"))

# calculate log odds ratios
# -------------------------
ratios$log.odds.ratio <- log(ratios$odds.ratio)

# add debate state date
ratios$debate_dateStart <- sort(unique(data$debate_dateStart))

# plot log odds ratios over time
p <- ggplot(data=ratios, aes(x=debate_dateStart, y=log.odds.ratio, group=group, color=group)) +
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank(),
          axis.line = element_line(colour = "black")) +
    geom_point() +
    geom_line() +
    xlab("") +
    ylab("Log-odds ratio") +
    guides(color = guide_legend(title = "Group:")) +
    scale_x_date(breaks = sort(unique(ratios$debate_dateStart)), labels=sort(unique(ratios$budget.year))) +
    theme(axis.text.x = element_text(angle = 30)) +
    scale_y_continuous(breaks=seq(-2.5,1,0.25)) +
    theme(legend.position = "bottom") +
    geom_hline(yintercept = 0, linetype="dashed", col="black") +     
    geom_vline(xintercept = as.numeric(sort(unique(as.Date(data$govtStart)))), dcolor="black", linetype="solid", col="grey") +     
    annotate("text", x=as.Date("1988-05-15"), y=1.5, label="FF Minority\n", size=4) +
    annotate("text", x=as.Date("1992-04-01"), y=1.5, label="FF Coalitions\n", size=4) +
    annotate("text", x=as.Date("1996-04-01"), y=1.5, label="FG-Lab-DL\nCoalition", size=4) +
    annotate("text", x=as.Date("2002-09-01"), y=1.5, label="FF-led Boom Years\n", size=4) +
    annotate("text", x=as.Date("2009-10-01"), y=1.5, label="FF-Greens\nCrisis Years", size=4) +
    annotate("text", x=as.Date("2012-08-01"), y=1.5, label="FG-Lab\nCrisis Years", size=4)

pdf(file="./plots/speaker_log_odds_ratios_over_time.pdf", height=6, width=12)
print(p)
dev.off()



